#coding=utf-8
import numpy as np

filename = "Plottest.txt" # 要求txt文件和当前脚本在同一目录下
decade = []
Countrynum = []
cost = []
with open(filename, 'r') as file_to_read:
  while True:
    lines = file_to_read.readline() # 整行读取数据
    if not lines:
      break
    time_temp, decade_temp, Countrynum_temp, cost_temp = [float(i) for i in lines.split()] # 将整行数据分割处理，如果分割符是空格，括号里就不用传入参数，如果是逗号， 则传入‘，'字符。
    decade.append(decade_temp)  # 添加新读取的数据
    Countrynum.append(Countrynum_temp)
    cost.append(cost_temp)
    pass
  decade = np.array(decade) # 将数据从list类型转换为array类型。
  Countrynum = np.array(Countrynum)
  cost = np.array(cost)



import matplotlib.pyplot as plt
import numpy as np

# 显示中文
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False

# 一张图
plt.figure()
plt.plot(decade,Countrynum,label='Country-decades')
plt.xlabel("decade")
plt.ylabel("Countrynum")
plt.legend(loc='best')
plt.show()

plt.figure()
plt.plot(decade,cost,label='cost-decades',color="red",linewidth=1.0,linestyle='--')

# 限制x,y轴的范围，设置标签
# plt.xlim((0,1000))
# plt.ylim((-2,8))
plt.xlabel("decade")
plt.ylabel("cost")

# 更换下标
# new_ticks = np.linspace(-2,3,10)
# plt.xticks(new_ticks)
# plt.yticks(range(5), ['cat', 'fish', 'dog$', 'tom', 'jerry'])

# # 移动x,y轴位置
# # gca = "get current axis"
# ax = plt.gca()
# ax.spines['right'].set_color('none') # 右边框设置成无颜色
# ax.spines['top'].set_color('none') # 上边框设置成无颜色
# ax.xaxis.set_ticks_position('bottom') # x轴用下边框代替，默认是这样
# ax.yaxis.set_ticks_position('left') # y轴用左边的边框代替，默认是这样
# ax.spines['bottom'].set_position(('data',0)) # x轴在y轴，０的位置
# ax.spines['left'].set_position(('data',0)) # y轴在x轴，０的位置

plt.legend(loc='best')
plt.show()